<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item style="width: 160px;">
        <el-input v-model="dataForm.recipientName" placeholder="输入受助者姓名" clearable></el-input>
      </el-form-item>
      <el-form-item style="width: 160px;">
        <el-select v-model="dataForm.emergencyGrade" placeholder="紧急程度" clearable>
          <el-option v-for="item in emergencyGradeType" :key="item.key" :label="item.value" :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="width: 160px;">
        <el-input v-model="dataForm.startAge" placeholder="开始年龄" clearable></el-input>
      </el-form-item>
      <el-form-item style="width: 160px;">
        <el-input v-model="dataForm.endAge" placeholder="结束年龄" clearable></el-input>
      </el-form-item>
      <el-form-item style="width: 160px;">
        <el-select v-model="dataForm.nurseGrade" placeholder="护理类别" clearable>
          <el-option v-for="item in options" :key="item.key" :label="item.value" :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="width: 160px;">
        <el-select v-model="dataForm.isSlove" placeholder="需求是否解决" clearable>
          <el-option v-for="item in solveType" :key="item.key" :label="item.value" :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新建</el-button>
      </el-form-item>
      <el-form-item>
        <el-select style="width: 150px;margin-left: 20px" v-model="dataForm.categoryId" clearable placeholder="受助类别">
          <el-option v-for="item in categorys" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="createTime" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss" range-separator="至"
          start-placeholder="开始需求建立时间" end-placeholder="结束需求建立时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="time" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss" range-separator="至"
          start-placeholder="开始需求解决时间" end-placeholder="结束需求解决时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column prop="recipientName" header-align="center" align="center" label="受助者姓名">
      </el-table-column>
      <el-table-column prop="categoryName" header-align="center" align="center" label="类别">
      </el-table-column>
      <el-table-column prop="emergencyGrade" header-align="center" align="center" label="紧急程度">
      </el-table-column>
      <el-table-column prop="gender" header-align="center" align="center" label="性别">
        <template v-slot:default="scope">
          {{ scope.row.gender == 0 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="age" header-align="center" align="center" label="年龄">
      </el-table-column>
      <el-table-column prop="nurseGrade" header-align="center" align="center" label="护理类别">
        <template v-slot:default="scope">
          <span v-if="scope.row.nurseGrade===0">全自理</span>
          <span v-else-if="scope.row.nurseGrade===1">半失能</span>
          <span v-else-if="scope.row.nurseGrade===2">失能</span>
          <span v-else>正常</span>
        </template>
      </el-table-column>
      <el-table-column prop="detail" header-align="center" align="center" label="需求描述">
      </el-table-column>
      <el-table-column prop="isSlove" header-align="center" align="center" label="该需求是否解决">
        <template v-slot:default="scope">
          {{ scope.row.isSlove === 0 ? '未解决' : '已解决' }}
        </template>
      </el-table-column>
      <el-table-column prop="fellowName" header-align="center" align="center" label="对接人姓名">
      </el-table-column>
      <el-table-column prop="fellowTel" header-align="center" align="center" label="对接人电话">
      </el-table-column>
      <el-table-column prop="establishTime" header-align="center" align="center" label="需求建立时间">
      </el-table-column>
      <el-table-column prop="solveTime" header-align="center" align="center" label="需求解决时间">
      </el-table-column>
      <el-table-column prop="helpRemark" header-align="center" align="center" label="帮助描述">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template v-slot:default="scope">
          <el-button type="primary" size="small" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button type="primary" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './need-add-or-update'
import service from "@/utils/httpRequest";

export default {
  data() {
    return {
      emergencyGradeType: [
        {
          key: 1,
          value: '第一等级'
        },
        {
          key: 2,
          value: '第二等级'
        },
        {
          key: 3,
          value: '第三等级'
        },
      ],
      categorys: [
        {
          id: 0,
          name: ''
        }
      ],
      solveType: [
        {
          key: 0,
          value: '未解决'
        },
        {
          key: 1,
          value: '已解决'
        }
      ],
      options: [
        {
          key: 0,
          value: '全自理',
        },
        {
          key: 1,
          value: '半失能',
        },
        {
          key: 2,
          value: '失能',
        },
        {
          key: 3,
          value: '正常'
        }
      ],
      createTime: [],
      time: [],
      dataForm: {
        categoryId: '',
        recipientName: '',
        startAge: '',
        endAge: '',
        emergencyGrade: '',
        nurseGrade: '',
        isSlove: '',
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    getCategorys() {
      service.get('/lovehelp/category/allcategory').then((response) => {
        if (response && response.code === 200) {
          this.categorys = response.categorys
        } else {
          this.categorys = []
        }
      }).catch((error) => {
        console.error(error)
      })
    },
    // 获取数据列表
    getDataList() {
      this.getCategorys()
      this.dataListLoading = true
      service({
        method: 'GET',
        url: '/lovehelp/need/list',
        params: {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'recipientName': this.dataForm.recipientName,
          'startCreateTime': this.createTime ? this.createTime[0] : '',
          'endCreateTime': this.createTime ? this.createTime[1] : '',
          'startAge': this.dataForm.startAge,
          'endAge': this.dataForm.endAge,
          'emergencyGrade': this.dataForm.emergencyGrade,
          'nurseGrade': this.dataForm.nurseGrade,
          'isSlove': this.dataForm.isSlove,
          'startSolveTime': this.time ? this.time[0] : '',
          'startEndTime': this.time ? this.time[1] : '',
          'categoryId': this.dataForm.categoryId
        }
      }).then(res => {
        if (res.code === 200) {
          this.dataList = res.page.list
          this.totalPage = res.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    }
    ,
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    }
    ,
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    }
    ,
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    }
    ,
    // 删除
    deleteHandle(id) {
      const ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      });
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        service({
          method: 'POST',
          url: '/lovehelp/need/delete',
          data: ids
        }).then(res => {
          if (res.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(res.msg);
          }
        })
      }).catch(() => {
        this.$message.info("已取消删除")
      })
    }
  }
}
</script>
